package com.huawei.esdk.oa.device.ad;

import java.lang.reflect.Proxy;
import org.apache.log4j.Logger;
import com.huawei.esdk.oa.device.ad.common.LdapConnectionFactory;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.nemgr.base.DeviceConnectionBase;
import com.huawei.esdk.platform.nemgr.base.MultiConnDeviceBase;

public class ADConnection extends DeviceConnectionBase
{

	private MultiConnDeviceBase device;
	
    private static final Logger LOGGER = Logger.getLogger(ADConnection.class);
    
	public ADConnection(MultiConnDeviceBase serviceProxy, String sap, String loginUser, String loginPwd)
	{
		super(loginUser, loginPwd);
		
        this.device = serviceProxy;
        
        try
        {
            LdapConnectionFactory.initConnection(sap, loginUser, loginPwd);
        }
        catch (Exception e)
        {
            LOGGER.error("AD DEVICE CONN ERROR", e);
        }
	}

	@Override
	public Object getServiceProxy(Class<?>[] itfs) throws SDKException {
        if (itfs.length == 1)
        {
            if (itfs[0].isInstance(device))
            {
                return device;
            }
            return device.getService(itfs[0]);
        }
        else
        {
            LOGGER.debug("Intefaces number is not 1");
            return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                    itfs, device.getService(itfs));
        }
	}

	@Override
	public boolean doHeartbeat(String connId) {
		return true;
	}

	@Override
	public boolean initConn(String connId) {
		return true;
	}

	@Override
	public void destroyConn(String connId) {
		LdapConnectionFactory.destroyConn();
		return;
	}
   
}
